package com.xinqi.modules.course.clazz.convert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.utils.json.JSON;
import com.xinqi.common.core.utils.StringUtils;
import com.xinqi.common.mapstruct.CommonConvertConfig;
import com.xinqi.modules.course.clazz.domain.ClassJoinAuditEntity;
import com.xinqi.modules.course.clazz.dto.ClassStudentAuditData;
import com.xinqi.modules.course.clazz.dto.req.ClassAuditJsonParamDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditCreateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassJoinAuditUpdateDTO;
import com.xinqi.modules.course.clazz.dto.req.ClassStudentCreateDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassJoinAuditResultDTO;
import com.xinqi.modules.course.course.enums.ClassAuditJsonParamEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.*;

/**
 * 学生加入班级审核转换
 *
 * @author: system
 * @date: 2023/01/17
 */
@Slf4j
@Mapper(config = CommonConvertConfig.class)
public abstract class ClassJoinAuditConvert {
    public abstract ClassJoinAuditEntity create(ClassJoinAuditCreateDTO s);

    public abstract ClassJoinAuditEntity update(ClassJoinAuditUpdateDTO s, @MappingTarget ClassJoinAuditEntity t);

    public abstract ClassJoinAuditEntity update(ClassJoinAuditUpdateDTO s);

    public abstract ClassJoinAuditResultDTO convert(ClassJoinAuditEntity s);

    public abstract ClassJoinAuditCreateDTO accept(ClassJoinAuditEntity s);

    public abstract ClassJoinAuditCreateDTO reject(ClassJoinAuditEntity s);

    public abstract ClassJoinAuditUpdateDTO convertUpdate(ClassJoinAuditResultDTO s);

    @Mapping(target = "auditContent", ignore = true)
    public abstract ClassJoinAuditCreateDTO convert(ClassStudentCreateDTO s);


    @AfterMapping
    public void after(ClassStudentCreateDTO s, @MappingTarget ClassJoinAuditEntity t) {
        if (!Objects.isNull(s.getAuditContent())) {
            t.setAuditContent(JSON.stringify(s.getAuditContent().getDataList()));
        }
    }

    public ClassAuditJsonParamDTO convertParam(ClassStudentAuditData dto) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(dto) && CollectionUtils.isNotEmpty(dto.getDataList()), "审核数据不能为空");

        ClassAuditJsonParamDTO classAuditJsonParamDTO = new ClassAuditJsonParamDTO();
        dto.getDataList().forEach(data -> {
            if (Objects.equals(ClassAuditJsonParamEnum.学生真实姓名.getCode(), data.getKey())) {
                classAuditJsonParamDTO.setStudentName(String.valueOf(data.getValue()));
            }

            if (Objects.equals(ClassAuditJsonParamEnum.与学生的关系.getCode(), data.getKey())) {
                classAuditJsonParamDTO.setRelationship(String.valueOf(data.getValue()));
            }

            if (Objects.equals(ClassAuditJsonParamEnum.学生绑定手机号.getCode(), data.getKey())) {
                classAuditJsonParamDTO.setMobile(String.valueOf(data.getValue()));
            }
        });

        return classAuditJsonParamDTO;
    }

    public String convertToString(ClassAuditJsonParamDTO jsonParam) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put(ClassAuditJsonParamEnum.学生真实姓名.getCode(), jsonParam.getStudentName());
        dataMap.put(ClassAuditJsonParamEnum.学生绑定手机号.getCode(), jsonParam.getMobile());
        dataMap.put(ClassAuditJsonParamEnum.与学生的关系.getCode(), jsonParam.getRelationship());
        //其余的报名信息
        if (MapUtils.isNotEmpty(jsonParam.getRestParamMap())) {
            dataMap.putAll(jsonParam.getRestParamMap());
        }
        return JSON.stringify(dataMap);
    }

    public String convertToArrayString(ClassAuditJsonParamDTO jsonParam) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> studentMap = new HashMap<>();
        mapList.add(studentMap);
        studentMap.put(ClassAuditJsonParamEnum.学生真实姓名.getCode(), jsonParam.getStudentName());

        Map<String, Object> mobileMap = new HashMap<>();
        mapList.add(mobileMap);
        mobileMap.put(ClassAuditJsonParamEnum.学生绑定手机号.getCode(), jsonParam.getMobile());

        Map<String, Object> relationshipMap = new HashMap<>();
        mapList.add(relationshipMap);
        relationshipMap.put(ClassAuditJsonParamEnum.与学生的关系.getCode(), jsonParam.getRelationship());

        //其余的报名信息
        if (MapUtils.isNotEmpty(jsonParam.getRestParamMap())) {
            Map<String, Object> restMap = new HashMap<>();
            for (Map.Entry<String, String> entry : jsonParam.getRestParamMap().entrySet()) {
                if (!Objects.equals(ClassAuditJsonParamEnum.学生真实姓名.getCode(), entry.getKey())
                    && !Objects.equals(ClassAuditJsonParamEnum.与学生的关系.getCode(), entry.getKey())
                    && !Objects.equals(ClassAuditJsonParamEnum.学生绑定手机号.getCode(), entry.getKey())) {
                    restMap.put(entry.getKey(), entry.getValue());
                }
            }

            if (MapUtils.isNotEmpty(restMap)) {
                mapList.add(restMap);
            }
        }

        return JSON.stringify(mapList);
    }

    public ClassAuditJsonParamDTO convertToParam(String param) {
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(param), "审核数据不能为空");
        ClassAuditJsonParamDTO classAuditJsonParamDTO = new ClassAuditJsonParamDTO();
        Map<String, String> restParamMap = new HashMap<>();

        Map<String, String> allMap = this.paraMap(param);
        String studentName = allMap.get(ClassAuditJsonParamEnum.学生真实姓名.getCode());
        String relationship = allMap.get(ClassAuditJsonParamEnum.与学生的关系.getCode());
        String mobile = allMap.get(ClassAuditJsonParamEnum.学生绑定手机号.getCode());

        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(studentName), "学生真实姓名不能为空");
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(relationship), "学生绑定手机号不能为空");
        CommonErrors.BAD_REQUEST.check(StringUtils.isNotEmpty(mobile), "与学生的关系不能为空");

        classAuditJsonParamDTO.setStudentName(studentName);
        classAuditJsonParamDTO.setRelationship(relationship);
        classAuditJsonParamDTO.setMobile(mobile);

        for (Map.Entry<String, String> entry : allMap.entrySet()) {
            if (!Objects.equals(ClassAuditJsonParamEnum.学生真实姓名.getCode(), entry.getKey())
                && !Objects.equals(ClassAuditJsonParamEnum.与学生的关系.getCode(), entry.getKey())
                && !Objects.equals(ClassAuditJsonParamEnum.学生绑定手机号.getCode(), entry.getKey())) {
                restParamMap.put(entry.getKey(), entry.getValue());
            }
        }

        classAuditJsonParamDTO.setRestParamMap(restParamMap);
        return classAuditJsonParamDTO;
    }

    public Map<String, String> paraMap(String jsonArrayString) {
        JSONArray jsonArray = com.alibaba.fastjson.JSON.parseArray(jsonArrayString);

        Map<String, String> resultMap = new HashMap<>();
        for (Object obj : jsonArray) {
            if (obj instanceof JSONObject) {
                JSONObject jsonObj = (JSONObject) obj;
                for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {
                    resultMap.put(entry.getKey(), String.valueOf(entry.getValue()));
                }
            }
        }

        return resultMap;
    }
}
